package com.movitech.mobile.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.movitech.mobile.cz.base.modules.config.CallBackAbutmentTypeEnum;
import com.movitech.mobile.cz.base.purchase.bizvo.PContactPlanVo;
import com.movitech.mobile.cz.base.purchase.entity.PContactPlan;
import com.movitech.mobile.cz.base.purchase.service.PContactPlanService;
import com.movitech.mobile.cz.base.purchase.service.SAbutmentLogsService;
import com.movitech.mobile.cz.base.sys.entity.ext.SCompanyRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SProjectRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SRegionCompanyRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SysUserRequestVO;
import com.movitech.mobile.cz.base.sys.service.SCompanyService;
import com.movitech.mobile.cz.base.sys.service.SProjectService;
import com.movitech.mobile.cz.base.sys.service.SRegionalCompanyService;
import com.movitech.mobile.cz.base.sys.service.SysUserService;
import com.movitech.mobile.mq.RabbitmqConllection;
import com.movitech.mobile.mq.RabbitmqConsumer;
import com.movitech.mobile.task.model.CompanyVoForMQ;
import com.movitech.mobile.task.model.ContractPlanVoForMQ;
import com.movitech.mobile.task.model.UserMq;
import com.movitech.utils.LoggerManager;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 合约规划MQ消息处理
 *
 * @author ldz
 * @version 1.0.0
 * @description 合约规划MQ消息处理
 * @createTime 2022/03/31
 * @copyright Copyright ©️ 2022 北京魔马科技
 */
@Component
public class ContractPlanSynRabbitMQConsumer implements RabbitmqConsumer {
    public static LoggerManager log = LoggerManager.get(ContractPlanSynRabbitMQConsumer.class);

    /**
     * MQ服务地址
     */
    @Value("${mqserver.contractplan.url}")
    private String MQ_SERVER_URL;

    /**
     * MQ服务密码
     */
    @Value("${mqserver.contractplan.port}")
    private String MQ_SERVER_PORT;

    /**
     * MQ服务账号
     */
    @Value("${mqserver.contractplan.user}")
    private String MQ_SERVER_USER;

    /**
     * MQ服务密码
     */
    @Value("${mqserver.contractplan.password}")
    private String MQ_SERVER_PASSWORD;

    @Value("${mqserver.contractplan.queue}")
    private String MQ_QUEUE_CONTRACTPLAN;

    @Autowired
    PContactPlanService pContactPlanService;

    @Autowired
    @Qualifier("sAbutmentLogsService")
    private SAbutmentLogsService sAbutmentLogsService;

    @Override
    public void init() {
        RabbitmqConllection rabbitmqConllection = RabbitmqConllection
                .instance(MQ_SERVER_URL,
                        MQ_SERVER_PORT,
                        MQ_SERVER_USER,
                        MQ_SERVER_PASSWORD);
        rabbitmqConllection.consumer(MQ_QUEUE_CONTRACTPLAN, this);
    }

    @Override
    public boolean consume(String queueName, String data) {
        if (MQ_QUEUE_CONTRACTPLAN.equals(queueName)) {
            sAbutmentLogsService.saveAbutmentLogs(CallBackAbutmentTypeEnum.CallBackAbutmentTypeEnum_8.getCode(), "", data, 1, 0);
            //
            List<ContractPlanVoForMQ> contractPlanVoList = new ArrayList<>();
            try {
                if (data.startsWith("[")) {
                    try {
                        contractPlanVoList = JSONArray.parseArray(data, ContractPlanVoForMQ.class);
                    } catch (JSONException e) {
                        log.error("数据格式错误：error=" + e.getMessage());
                        return true;
                    }
                } else if (data.startsWith("{")) {
                    try {
                        ContractPlanVoForMQ contractPlanVoForMQ = JSONObject.parseObject(data, ContractPlanVoForMQ.class);
                        contractPlanVoList.add(contractPlanVoForMQ);
                    } catch (Exception ex) {
                        log.error("数据格式错误：error=" + ex.getMessage());
                        return true;
                    }
                } else {
                    log.error("数据格式错误：data=" + data);
                    return true;
                }
                if (contractPlanVoList != null && contractPlanVoList.size() > 0) {

                    List<PContactPlanVo> contactPlanList = new ArrayList<>();
                    for (ContractPlanVoForMQ contractPlanVoForMQ : contractPlanVoList) {
                        PContactPlanVo pContactPlan = new PContactPlanVo();
                        pContactPlan.setProjectByStageCode(contractPlanVoForMQ.getProjectCode());
                        pContactPlan.setProjectByStage(contractPlanVoForMQ.getProjectName());
                        pContactPlan.setContractCode(contractPlanVoForMQ.getCode());
                        pContactPlan.setContractId(contractPlanVoForMQ.getUuid());
                        pContactPlan.setContractName(contractPlanVoForMQ.getName());
                        pContactPlan.setContractDetailId(contractPlanVoForMQ.getTargetCostDataUuids());
                        pContactPlan.setDelFlag(contractPlanVoForMQ.getIsDelete());
                        pContactPlan.setPlanBudgetPrices(contractPlanVoForMQ.getTotalTargetCost());
                        pContactPlan.setPurchaseType(contractPlanVoForMQ.getProcurementMethod());
                        contactPlanList.add(pContactPlan);
                    }
                    pContactPlanService.batchSave(contactPlanList);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            }

        }
        return true;
    }

    @Override
    public void error(String queueName, String message, Throwable throwable) {

    }


}
